package com.example;

import com.example.constant.KafkaTopic;
import com.example.entity.OrderDetail;
import com.example.entity.User;
import io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig;
import io.confluent.kafka.serializers.KafkaAvroDeserializer;
import io.confluent.kafka.serializers.KafkaAvroSerializer;
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Iterator;
import java.util.Properties;

public class KafkaConsumerDetailExample {


    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.8.80:9092,192.168.8.80:9093,192.168.8.80:9094");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class.getName());
        props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://192.168.8.80:8081");
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "test1111");
        props.put("specific.avro.reader", true);
        Consumer<String, OrderDetail> producer = new KafkaConsumer<>(props);
        producer.subscribe(Collections.singleton(KafkaTopic.orderDetail));
        while (true){
            ConsumerRecords<String, OrderDetail> poll = producer.poll(1000);
            if (!poll.isEmpty()){
                Iterator<ConsumerRecord<String, OrderDetail>> iterator = poll.iterator();
                while (iterator.hasNext()){
                    OrderDetail orderDetail = iterator.next().value();
                    System.out.println(("收到了"+orderDetail));
                }
            }
        }
    }
}